from sqlalchemy.orm import Session
from schema import System_Log_schema
from model import System_Log_model



def get_System_Log(db: Session, ID: int):
    return db.query(System_Log_model.System_log).filter(System_Log_model.System_log.ID == ID).first()


def get_all_System_Log(db: Session):
    return db.query(System_Log_model.System_log).all()


def delete_System_Log(db: Session, ID):
    to_delete = db.query(System_Log_model.System_log).filter(System_Log_model.System_log.ID == ID)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_System_Log(db: Session, System_Log: System_Log_schema.System_log):
    db_System_Log = System_Log_model.System_log(
        AdminID=System_Log.AdminID,
        LesseeID=System_Log.LesseeID,
        OperationType=System_Log.OperationType,
        OperationDescription=System_Log.OperationDescription,
        OperationTime=System_Log.OperationTime,
    )
    db.add(db_System_Log)
    db.commit()
    db.refresh(db_System_Log)
    return db_System_Log


def update_System_Log(db: Session, ID, field_name, new_value):
    db_System_Log = db.query(System_Log_model.System_log).filter(System_Log_model.System_log.ID == ID).first()
    setattr(db_System_Log, field_name, new_value)
    db.commit()
    db.refresh(db_System_Log)
    return db_System_Log
